package com.kcl.dfss;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;

import com.kcl.dfss.fragments.PersonalFragment;
import com.kcl.dfss.fragments.StaticsFragment;
import com.kcl.dfss.fragments.SurveillanceFragment;
import com.kcl.dfss.http.CameraConnection;
import com.kcl.dfss.http.CameraConnection.ResponseHandler;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

public class MainActivity extends FragmentActivity implements
		View.OnClickListener {

	private ImageView mStaticsView;
	private ImageView mSurveiView;
	private ImageView mPersonalView;

	private static final int ID_STATICS = 0;
	private static final int ID_SURVEILANCE = 1;
	private static final int ID_PERSONAL = 2;

	private static PersonalFragment mPersonalFrag = null;
	private static SurveillanceFragment mSurveilanceFrag = null;
	private static StaticsFragment mStaticsFrag = null;

	// ����FragmentManager����
	FragmentManager fManager;
	FragmentTransaction fTransaction;
	private static final String TAG = "MainActivity";

	private CameraConnection mCameraConnection = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		fManager = getSupportFragmentManager();
		initUI();
		setFragment(0);
		Log.d("Kyle", "onCreate");
		mCameraConnection = new CameraConnection(this, new ResponseHandler() {
			@Override
			public void handleResponse(int request_code, String response) {
				// TODO Auto-generated method stub
				switch (request_code) {
				case CameraConnection.CODE_FIRMWARE_VERSION:
					if (!response.equalsIgnoreCase("error")) {
						Log.d(TAG, "get camera firmware version is " + response);
						if (LocationService.getInstance() == null) {
							Intent intent = new Intent();
							intent.setClass(getBaseContext(),
									LocationService.class);
							intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							startService(intent);
						}
					}
					break;
				default:
					break;
				}
			}
		});
		startServiceIfNeeded();
	}

	private void startServiceIfNeeded() {
		if (mCameraConnection.isCameraAvailable()) {
			mCameraConnection.getFirmwareVersion();
		}
	}

	public void initUI() {
		mStaticsView = (ImageView) findViewById(R.id.statics);
		mSurveiView = (ImageView) findViewById(R.id.surveilance);
		mPersonalView = (ImageView) findViewById(R.id.personal);

		mStaticsView.setOnClickListener(this);
		mSurveiView.setOnClickListener(this);
		mPersonalView.setOnClickListener(this);

	}

	public void setFragment(int index) {
		// ����ѡ��+��������Fragment
		fTransaction = fManager.beginTransaction();
		clearChioce();
		hideFragments(fTransaction);
		switch (index) {
		case 0:
			mStaticsView.setImageResource(R.drawable.tongji_after);
			if (mStaticsFrag == null) {
				// ���fg1Ϊ�գ��򴴽�һ������ӵ�������
				mStaticsFrag = StaticsFragment.getInstance();
				fTransaction.add(R.id.simple_fragment, mStaticsFrag);
			} else {
				// ���MessageFragment��Ϊ�գ���ֱ�ӽ�����ʾ����
				fTransaction.show(mStaticsFrag);
			}
			break;

		case 1:
			mSurveiView.setImageResource(R.drawable.jiankong_after);
			if (mSurveilanceFrag == null) {
				// ���fg1Ϊ�գ��򴴽�һ������ӵ�������
				mSurveilanceFrag = SurveillanceFragment.getInstance();
				fTransaction.add(R.id.simple_fragment, mSurveilanceFrag);
			} else {
				// ���MessageFragment��Ϊ�գ���ֱ�ӽ�����ʾ����
				fTransaction.show(mSurveilanceFrag);
			}
			break;

		case 2:
			mPersonalView.setImageResource(R.drawable.pc_after);
			if (mPersonalFrag == null) {
				// ���fg1Ϊ�գ��򴴽�һ������ӵ�������
				mPersonalFrag = PersonalFragment.getInstance();
				fTransaction.add(R.id.simple_fragment, mPersonalFrag);
			} else {
				// ���MessageFragment��Ϊ�գ���ֱ�ӽ�����ʾ����
				fTransaction.show(mPersonalFrag);
			}
			break;
		}
		fTransaction.commit();
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.statics:
			setFragment(ID_STATICS);
			break;
		case R.id.personal:
			setFragment(ID_PERSONAL);
			break;
		case R.id.surveilance:
			setFragment(ID_SURVEILANCE);
			break;
		default:
			break;
		}
	}

	// �������е�Fragment,����fragment����
	private void hideFragments(FragmentTransaction transaction) {
		if (mPersonalFrag != null) {
			transaction.hide(mPersonalFrag);
		}
		if (mSurveilanceFrag != null) {
			transaction.hide(mSurveilanceFrag);
		}
		if (mStaticsFrag != null) {
			transaction.hide(mStaticsFrag);
		}
	}

	// ����һ����������ѡ��ķ���
	public void clearChioce() {
		mStaticsView.setImageResource(R.drawable.tongji_before);
		mSurveiView.setImageResource(R.drawable.jiankong_before);
		mPersonalView.setImageResource(R.drawable.pc_before);
	}

}
